package com.youbug.mall.product_pms.service.impl;

import com.youbug.mall.product_pms.entity.PmsProductAttrValue;
import com.youbug.mall.product_pms.mapper.PmsProductAttrValueMapper;
import com.youbug.mall.product_pms.service.IPmsProductAttrValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * spu属性值 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-04-21
 */
@Service
public class PmsProductAttrValueServiceImpl extends ServiceImpl<PmsProductAttrValueMapper, PmsProductAttrValue> implements IPmsProductAttrValueService {

        @Override
        public IPage<PmsProductAttrValue> getPmsProductAttrValuePage(QueryVo<PmsProductAttrValue> queryVo) {

        Page<PmsProductAttrValue> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<PmsProductAttrValue> wrapper = new QueryWrapper<>();

        PmsProductAttrValue pmsProductAttrValue = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(pmsProductAttrValue)){
            if(StringUtils.hasText(pmsProductAttrValue.getAttrName())){
                wrapper.eq("attr_name", pmsProductAttrValue.getAttrName());
            }
            if(StringUtils.hasText(pmsProductAttrValue.getAttrValue())){
                wrapper.eq("attr_value", pmsProductAttrValue.getAttrValue());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<PmsProductAttrValue> page = this.page(paramPage, wrapper);

        return page;
        }

    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public boolean saveBatch(Collection<PmsProductAttrValue> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    @Override
    public List<PmsProductAttrValue> getPmsSpuBaseAttrs(Long id) {
        List<PmsProductAttrValue> values = this.list(new QueryWrapper<PmsProductAttrValue>().eq("spu_id", id));

        return values;
    }

    @Override
    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public boolean updateSpuBaseAttrValues(List<PmsProductAttrValue> attrValues) {

        long spuId = attrValues.get(0).getSpuId();

        this.remove(new QueryWrapper<PmsProductAttrValue>().eq("spu_id", spuId));

        this.saveBatch(attrValues);

        return true;
    }

}
